package top.kaoshanji.p1ch03b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import top.kaoshanji.u1constant.FileConstant;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 代码清单 p1ch03b-13：向异步文件通道中写入数据的示例
 * 基于 Future 类的实现方式
 * 并未执行
 * author kaoshanji
 * ^_^
 * create time 2025/1/11 19:51
 */
public class AsynchronousFileChannelExample01 {

    private static final Logger logger = LogManager.getLogger(AsynchronousFileChannelExample01.class);

    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        logger.info("...代码清单 p1ch03b-13：向异步文件通道中写入数据的示例...");

        AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get(FileConstant.LOCAL_ROOT_PATH.concat("p1ch03b13-large.bin")),
                StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        ByteBuffer buffer = ByteBuffer.allocate(32 * 1024 * 1024);
        Future<Integer> result = channel.write(buffer, 0);

        // 其他操作
        Integer len = result.get();

    }

}
